app.controller("column",function($scope,$http,get,ngTreetableParams,$uibModal,$rootScope,del,$state){
	$scope.search=function(){
		get.list('menu/privAll').then(function(e) {
		  	$scope.data=e;
		  	$scope.expanded_params1= new ngTreetableParams({//treetable插件
		        getNodes: function(parent) {
		              return parent ? parent.privileges :$scope.data;
		        },
		        getTemplate: function(node) {
		            return 'tree_node';
		        },          
		        options: {
		          /*  initialState: 'expanded'  */  
		        }
		    })
		  });
		  
	}
	$scope.search();
	/**
	 * 增加与修改
	 * @param {Object} id
	 * @param {Object} tab
	 */
	$scope.columnAdd = function(id,tab) {
		$uibModal.open({ //模态框
			animation: true,
			templateUrl: 'columnAdd.html',
			size: 'lg',
			 backdrop: 'static' ,
			controller: 'columnAdd',
			resolve: { //这是一个入参,这个很重要,它可以把主控制器中的参数传到模态框控制器中
				id: function() { //是一个回调函数
					return id; //这个值会被模态框的控制器获取到
				},
				tab:function (){
					return tab;
				},
				scope:function(){
					return $scope;
				}
			}
		}) //模态框
	}
	/**
	 * 删除
	 */
	$scope.modelClick = function(id) { //单删
	$uibModal.open({ //模态框
		animation: true,
		templateUrl: 'myModalDel.html',
		size: 'sm',
		 backdrop: 'static' ,
		controller: function($uibModalInstance) {
			var $ctrl = this;
				$ctrl.state = true;
			$ctrl.ok = function() {
				del.list("menu/privDel",{pId:id}).then(function(e){
					$uibModalInstance.close();
					$rootScope.open(e);
					$state.go("home.column",null,{
				     		reload:true
				     	})
				});
				
			}; //ok  
			$ctrl.cancel = function() {
				$uibModalInstance.close();
			};
		}, //controller
		controllerAs: '$ctrl'
	})
	}
})
app.controller('columnAdd',function($scope,$rootScope,get,ngTreetableParams,$uibModalInstance,tab,id,$http,post,scope,$state){
	$scope.list={ids:''};
	$scope.id=id
	$http.get("json/icon/icon.json").then(function(e){
		$scope.icon=e.data
	})
	
	if(tab=='add'){
		$scope.sername="添加";
	}else{
		get.list("menu/privId",{pId:id}).then(function (e){
			console.log(e);
			$scope.name=e.pName||'';
			$scope.pUrl=e.pUrl||'';
			$scope.pPlace=e.pPlace||'';
			$scope.pRoute=e.pRoute||'';
			$scope.list.ids=e.pIcon||'';
			$scope.pStyle=e.pStyle;
			$scope.pPid=e.pPid;
		});
		$scope.sername="修改";
		
	}
	if($scope.id==0||$scope.href==''){
		$scope.type="menu"
	}else{
		$scope.type="permission"
	}
	$scope.ok=function (){
		if(tab=='add'){
			var param={pName:$scope.name||'',
			pUrl:$scope.pUrl||'',
			pPlace:$scope.pPlace||'',
			pRoute:$scope.pRoute||'',
			pIcon:$scope.list.ids,
			pStyle:$scope.pStyle,
			pPid:$scope.id||''
		};
			post.list("/menu/privAdd",param).then(function(e){
			$uibModalInstance.close();
			$rootScope.open(e);
			$state.go("home.column",null,{
				     		reload:true
				     	})
		});
		}else{
			var param={pName:$scope.name||'',
			pUrl:$scope.pUrl||'',
			pPlace:$scope.pPlace||'',
			pRoute:$scope.pRoute||'',
			pIcon:$scope.list.ids,
			pStyle:$scope.pStyle,
			pId:$scope.id,
			pPid:$scope.pPid||''
		};
		post.list("/menu/privUpd",
			param).then(function(e){
			$uibModalInstance.close();
			$rootScope.open(e);
			$state.go("home.column",null,{
				     		reload:true
				     	})
			
		});
		}
	}
	$scope.cancel = function() {
		$uibModalInstance.close();
	};
	
});